package com.itxiaoxiong.chat.mapper;

import com.itxiaoxiong.chat.pojo.GroupMembers;
import com.itxiaoxiong.chat.pojo.GroupMessages;
import com.itxiaoxiong.chat.pojo.Groups;
import com.itxiaoxiong.chat.pojo.Result;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface GroupMapper {

    /*
    根据用户id查找所在群列表
     */
    @Select("select * from group_members where user_id = #{userId}")
    List<GroupMembers> getGroupById(@Param("userId") Integer userId);

    @Select("select * from `groups` where group_id = #{groupId}")
    Groups getById(Integer groupId);

    /*
    查找用户是否在这个群
     */
    @Select("select count(*) > 0 from group_members where user_id = #{userId} and group_id = #{groupId}")
    boolean checkIfFriendExists(@Param("userId")Integer userId,@Param("groupId")Integer groupId);

    /*
    查找群的聊天记录
     */
    @Select("select * from group_messages where group_id = #{groupId}")
    List<GroupMessages> groupChatSelect(@Param("groupId")Integer groupId);

    /*
    发消息
     */
    @Insert("insert into group_messages (group_id, sender_id, message_content) values (#{groupId}, #{senderId}, #{messageContent})")
    void groupAddChat(GroupMessages groupMessages);

    /*
    根据群名查找群
     */
    @Select("select * from `groups` where group_name = #{groupName};")
    Groups groupName(@Param("groupName") String groupName);

    /*
    加群
     */
    @Insert("insert into group_members (group_id, user_id) values (#{groupId}, #{userId})")
    void groupAdd(GroupMembers groupMembers);

    /*
    退群
     */
    @Delete("delete from group_members where (group_id = #{groupId} and user_id = #{userId})")
    void groupDelete(GroupMembers groupMembers);

    /*
    发文件
     */
    @Insert("insert into group_messages (group_id, sender_id, message_content, image_content) values (#{groupId}, #{senderId}, #{messageContent}, #{imageContent})")
    void addGroupFile(GroupMessages groupMessages);

    /*
    查找群成员
     */
    @Select("select user_id from group_members where group_id = #{groupId}")
    List<Integer> getMemberByGroupId(@Param("groupId") Integer groupId);
}
